<!DOCTYPE html>
<html lang="zh-cn"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head th:replace="common::head"></head>
<body>
<div th:replace="common::top"></div>
<br/>
<h2>发货</h2>
<br/>

<table>
    <tr>
        <td><b>编码:</b></td>
        <td th:text="${order.code}"></td>
        <td colspan="2"></td>
        <td><b>订单总量:</b></td>
        <td th:text="${order.orderNum}"></td>
    </tr>
    <tr>
        <td><b>状态:</b></td>
        <td th:text="${order.status}==1?'正常':'撤销'"></td>
        <td><b>已上产线:</b></td>
        <td th:text="${order.confirm}?'是':'否'"></td>
        <td><b>生产总量:</b></td>
        <td th:text="${order.produceNum}"></td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td><b>销售:</b></td>
        <td th:text="${order.createUser}"></td>
        <td><b>发送总量:</b></td>
        <td th:text="${order.sendNum}"></td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td><b>创建时间:</b></td>
        <td th:text="${#dates.format(order.createTime,'yyyy-MM-dd')}"></td>
        <td><b>退货总量:</b></td>
        <td th:text="${order.refundNum}"></td>
    </tr>
</table>
<br/>
<form id="create" th:if="!${inSending}" th:action="@{create}" method="post">
    <input type="number" name="orderId" th:value="${order.id}" hidden>
    <input type="submit" value="创建货单(开始发货)">
</form>

<table th:each="item,stat:${map}">
    <tr><td colspan="4" th:text="'发货单('+${stat.current.key.id}+')'"></td></tr>
    <tr th:object="${stat.current.key}">
        <td>操作人:</td>
        <td th:text="*{createUser}"></td>
        <td th:text="*{#dates.format(createTime,'yyyy-MM-dd')}"></td>
        <td>
            <form class="complete" th:action="@{complete}" method="post">
                <input type="number" name="id" th:value="*{id}" hidden>
                <input type="submit" value="发货完成">
            </form>
        </td>
    </tr>
    <tr>
        <td>序号</td>
        <td>产品</td>
        <td>数量</td>
        <td>操作</td>
    </tr>
    <tr th:each="e : ${stat.current.value}">
        <td th:text="${eStat.index+1}"></td>
        <td th:text="${e.productName}"></td>
        <td th:text="${e.number}"></td>
        <td>
            <form class="add" th:action="@{add}" method="post">
                <input type="number" name="id" th:value="${e.id}" hidden>
                <input type="number" name="num" min="1" required style="width:50px;">
                <input type="submit" value="添加">
            </form>
        </td>
    </tr>
</table>
<p>
    <a href="javascript:history.back(-1);">上一页</a>
    <a href="../index.html" th:href="@{/index.html}">首页</a>
</p>

<script>
    $(".add").submit(function(e){
        e.preventDefault();
        var $show = $(this).parent().prev();

        var num = $(this).find("input:eq(2)").val();
        var number = $(e.target).parent().prev().text();
        var sum = parseInt(num) + parseInt(number);

        $.ajax({
            type: $(e.target).attr("method"),
            url: $(e.target).attr("action"),
            data: $(e.target).serialize() + '&sum=' + sum,
            success: function(response){
                $show.text(sum);
            },
            error: function(response){
                alert("提交失败");
            }
        });
    });
</script>
</body>
</html>